﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SRF.UI.View;

namespace SRF.Web.Controllers
{
    public class HomeController : Controller
    {
        protected override void Initialize(System.Web.Routing.RequestContext requestContext)
        {
            base.Initialize(requestContext);
        }
        [SRF.Security.Authorize]
        public ActionResult Index()
        {
            return View();
        } 

        public ActionResult Login()
        {
            if (SRF.Security.SecurityContext.IsLogin)
                return this.RedirectToHome();
            else
                return View();
        }
        [HttpPost]
        public ActionResult Login(string loginId, string password)
        {
            try
            {
                SRF.Security.SecurityContext.Login(loginId, password);
                if (!string.IsNullOrEmpty(Request["ReturnUrl"]))
                {
                    return Redirect(Request["ReturnUrl"]);
                }
                else
                {
                    return this.RedirectToHome();
                }
            }
            catch (System.Security.SecurityException ex)
            {
                string loginFailMessage = System.Configuration.ConfigurationManager.AppSettings["LoginFailMessage"];
                if (!string.IsNullOrEmpty(loginFailMessage))
                    ViewBag.ErrorMsg = loginFailMessage;
                else
                    ViewBag.ErrorMsg = ex.Message;

                return View();
            }
        }

        public ActionResult Logout()
        {
            Security.SecurityContext.Logout();

            return Redirect("/");
        }

        public ActionResult LoginDialog()
        {
            return View();
        }
    }
}